/******************************************************************************
 * FILE NAME:
 *  syslog_api_platform.h
 *
 * DESCRIPTION:
 * This file contains the functions of api-platform.
 *
 * DESIGN REFERENCE:
 *
 *
 * REVISION HISTORY   :
 * Date            Author               REASON
 * 06.08.2013      Thuan Nguyen         Creation
 *
 * Copyright 2005, NET
 * ***************************************************************************/


#include"../h/syslog_api_platform.h"
#include"../../transport/h/tp_ap_int_def.h"

/***********************************************************************
 * FUNCTION NAME:
 *  syslog_alloc
 * DESCRIPTION:
 *	This function handle allocate resource for pointer
 * INPUT:
 *
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
void* syslog_alloc(U32 caller_id,U32 block_size)
{
	void *ptr = NULL;
	ptr = malloc(block_size);
	if(NULL == ptr)
	{
		SYSLOG_COMMON_ERROR_LOGGING("[Caller:%d]Allocation memory failure %d", \
				caller_id,block_size);
		exit(EXIT_FAILURE);
	}
	return ptr;
}
/***********************************************************************
 * FUNCTION NAME:
 *  syslog_dealloc
 * DESCRIPTION:
 *	This function handle de-allocate resource for pointer
 * INPUT:
 *
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
void syslog_dealloc(U32 caller_id,void *block_ptr)
{
	if(NULL == block_ptr)
	{
		SYSLOG_COMMON_ERROR_LOGGING("[Caller:%d]Attempt to free NULL pointer %d", \
				caller_id,block_ptr);
	}else{
		free(block_ptr);
		block_ptr = NULL;
	}
}
/***********************************************************************
 * FUNCTION NAME:
 *  api_send_msg_queue
 * DESCRIPTION:
 *	API send message to message queue.
 *
 * INPUT:
 *
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
void api_send_msg_queue(U32 sender_id,U32 receiver_id,void *msg,U32 msg_len)
{
	U32 msgid;
	/*Validate message*/

	if((SYSLOG_NULL == msg) || (msg_len <= 0))
	{
		SYSLOG_COMMON_ERROR_LOGGING("Invalid Parameter");
	}
	else
	{

		switch(receiver_id)
		{
		case TASKLIST_MQUEUE:
			msgid = msgget((key_t)TASKLIST_MQUEUE, 0666 | IPC_CREAT);
			if( SYSLOG_FAILURE == msgid){
				SYSLOG_COMMON_ERROR_LOGGING("TASKLIST: msgget failure");
			}

			if(SYSLOG_FAILURE == msgsnd(msgid,(void*)msg,msg_len,0) )
			{
				SYSLOG_COMMON_ERROR_LOGGING("TASKLIST: msgsnd failure");
				msg = SYSLOG_NULL;
			}
			else
			{
				SYSLOG_COMMON_DEBUG_LOGGING("TASKLIST: msgsnd successful");
			}
			break;
		case TRANSPORT_INPUT_MQUEUE:
			msgid = msgget((key_t)TRANSPORT_INPUT_MQUEUE, 0666 | IPC_CREAT);
			if( SYSLOG_FAILURE == msgid){
				SYSLOG_COMMON_ERROR_LOGGING("INPUTQUEUE_TRANSPORT: msgget failure");
			}
			if(SYSLOG_FAILURE == msgsnd(msgid,(void*)msg,msg_len,0) )
			{

				SYSLOG_COMMON_ERROR_LOGGING("INPUTQUEUE_TRANSPORT: msgsnd failure");
				msg = SYSLOG_NULL;
			}
			else
			{
				SYSLOG_COMMON_DEBUG_LOGGING("INPUTQUEUE_TRANSPORT: msgsnd successful");
			}
			break;
		case APPLICATION_OUTPUT_MQUEUE:
			msgid = msgget((key_t)APPLICATION_OUTPUT_MQUEUE, 0666 | IPC_CREAT);
			if( SYSLOG_FAILURE == msgid){
				SYSLOG_COMMON_ERROR_LOGGING("APPLICATION_OUTPUT_MQUEUE: msgget failure");
			}
			if(SYSLOG_FAILURE == msgsnd(msgid,(void*)msg,msg_len,0) )
			{
				SYSLOG_COMMON_ERROR_LOGGING("APPLICATION_OUTPUT_MQUEUE: msgsnd failure");
				msg = SYSLOG_NULL;
			}
			else
			{
				SYSLOG_COMMON_DEBUG_LOGGING("APPLICATION_OUTPUT_MQUEUE: msgsnd successful");
			}
			break;
		default:
			SYSLOG_COMMON_ERROR_LOGGING("Receiver_id is wrong");
			msg = SYSLOG_NULL;
			break;
		}
	}
}
/***********************************************************************
 * FUNCTION NAME:
 *  api_receive_msg_queue
 * DESCRIPTION:
 *	API to receive message from message queue.
 *
 * INPUT:
 *
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
void* api_receive_msg_queue(U32 caller_id)
{
	U32 msgid;
	U32 msg_to_receive = 0;
	void *req_msg;
	/*Validate message*/


	switch(caller_id)
	{
		case TASKLIST_MQUEUE:
			req_msg = T_OS_BLOCK_ALLOC(TASK_ID_TRANSPORT_INPUTQUEUE,TRANSPORT_MESSEGE_MAX_LENGHT);
			msgid = msgget((key_t)TASKLIST_MQUEUE, 0666 | IPC_CREAT);
			if( SYSLOG_FAILURE == msgid){
				SYSLOG_COMMON_ERROR_LOGGING("TASKLIST: msgget failure");
				req_msg = SYSLOG_NULL;
			}
			if(SYSLOG_FAILURE == msgrcv(msgid,(void*)req_msg,TRANSPORT_MESSEGE_MAX_LENGHT,msg_to_receive,0) ){
					SYSLOG_COMMON_ERROR_LOGGING("TASKLIST: msgrcv failure");
					T_OS_BLOCK_DEALLOC(TASKLIST_MQUEUE,req_msg);
			}
			break;
		case TRANSPORT_INPUT_MQUEUE:
			req_msg = T_OS_BLOCK_ALLOC(TASK_ID_TRANSPORT_INPUTQUEUE,TRANSPORT_MESSEGE_MAX_LENGHT);

			msgid = msgget((key_t)TRANSPORT_INPUT_MQUEUE, 0666 | IPC_CREAT);
			if( SYSLOG_FAILURE == msgid){
				SYSLOG_COMMON_ERROR_LOGGING("TRANSPORT_INPUT_MQUEUE: msgget failure");
				req_msg = SYSLOG_NULL;
			}

			if(SYSLOG_FAILURE == msgrcv(msgid,(void*)req_msg,TRANSPORT_MESSEGE_MAX_LENGHT,msg_to_receive,0) ){
				SYSLOG_COMMON_ERROR_LOGGING("INPUTQUEUE_TRANSPORT: msgrcv failure");
				T_OS_BLOCK_DEALLOC(TASK_ID_TRANSPORT_INPUTQUEUE,req_msg);
				req_msg = SYSLOG_NULL;
			}
			break;
		case APPLICATION_OUTPUT_MQUEUE:
			req_msg = T_OS_BLOCK_ALLOC(TASK_ID_APPLICATION_INPUTQUEUE,TRANSPORT_MESSEGE_MAX_LENGHT);

			msgid = msgget((key_t)APPLICATION_OUTPUT_MQUEUE, 0666 | IPC_CREAT);
			if( SYSLOG_FAILURE == msgid){
				SYSLOG_COMMON_ERROR_LOGGING("APPLICATION_OUTPUT_MQUEUE: msgget failure");
				req_msg = SYSLOG_NULL;
			}
			if(SYSLOG_FAILURE == msgrcv(msgid,(void*)req_msg,TRANSPORT_MESSEGE_MAX_LENGHT,msg_to_receive,0) ){
				SYSLOG_COMMON_ERROR_LOGGING("APPLICATION_OUTPUT_MQUEUE: msgrcv failure");
				T_OS_BLOCK_DEALLOC(TASK_ID_APPLICATION_INPUTQUEUE,req_msg);
				req_msg = SYSLOG_NULL;
			}
			break;

		default:
			SYSLOG_COMMON_ERROR_LOGGING("Receiver_id is wrong");
			req_msg = SYSLOG_NULL;
			break;
	}
	return req_msg;
}
/***********************************************************************
 * FUNCTION NAME:
 *  api_send_msg_func
 * DESCRIPTION:
 *	API to send message.
 *
 * INPUT:
 *
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
U32 api_send_msg_func(U32 sender_id,U32 receiver_id,U8 socket_id,void *msg \
		,U32 msg_len,U8 protocol)
{
	U32 msgid;
	U32 val_result;
	/*Validate message*/
	if((SYSLOG_NULL == msg) || (msg_len <= 0))
	{
		SYSLOG_COMMON_ERROR_LOGGING("Invalid Parameter");
	}
	if(TRANSPORT_PROTOCOL_TCP == protocol)
	{
		val_result = write(socket_id,msg,msg_len);
		if(val_result < 0)
		{
			SYSLOG_COMMON_ERROR_LOGGING("Write failure with socket_id = %d",socket_id);
		}
	}
	else
	{
		if(TRANSPORT_PROTOCOL_UDP == protocol)
		{
			SYSLOG_COMMON_ERROR_LOGGING("This feature not support");
			SYSLOG_COMMON_ERROR_LOGGING("sending message failure");
		}
		else
		{
			SYSLOG_COMMON_ERROR_LOGGING("Unknow protocol");
		}
	}
	return val_result;
}
/***********************************************************************
 * FUNCTION NAME:
 *  api_receive_msg_func
 * DESCRIPTION:
 *	API to receive message.
 *
 * INPUT:
 *
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
U32 api_receive_msg_func(U32 caller_id,U8 socket_id,void *msg,U8 protocol)
{
	U32 msg_len;
	U32 val_result;
	/*Validate message*/

	msg_len = TRANSPORT_MESSEGE_MAX_LENGHT;

	if(socket_id < 0)
	{
		SYSLOG_COMMON_ERROR_LOGGING("Invalid Parameter");
	}
	if(TRANSPORT_PROTOCOL_TCP == protocol)
	{
		//Allocation for msg;
		msg = T_OS_BLOCK_ALLOC(TASK_ID_TRANSPORT,msg_len);

		val_result = read(socket_id,msg,msg_len);

		if(val_result < 0)
		{
			SYSLOG_COMMON_ERROR_LOGGING("Read failure with socket_id = %d",socket_id);
			T_OS_BLOCK_DEALLOC(TASK_ID_TRANSPORT,msg);
		}
	}
	else
	{
		if(TRANSPORT_PROTOCOL_UDP == protocol)
		{
			SYSLOG_COMMON_ERROR_LOGGING("This feature not support");
			SYSLOG_COMMON_ERROR_LOGGING("sending message failure");
		}
		else
		{
			SYSLOG_COMMON_ERROR_LOGGING("Unknow protocol");
		}
	}
	return val_result;
}
